package com.magictower.primary.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magictower.primary.R;
import com.magictower.primary.bean.CacheInfo;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author kejunzhan
 * @since 2017/12/26
 */

public class CacheAdapter extends BaseAdapter {
    private List<CacheInfo> cacheInfos;
    private Context ctx;
    public CacheAdapter(Context context, List<CacheInfo> cacheInfoList) {
        this.ctx = context;
        Collections.sort(cacheInfoList, new Comparator<CacheInfo>() {
            @Override
            public int compare(CacheInfo obj1, CacheInfo obj2) {
                //第一比较值
                long i = obj2.getCacheSize() - obj1.getCacheSize();
                if(i == 0){
                    //第二比较值
                    i = obj2.getDataSize() - obj1.getDataSize();
                }
                if (i > Integer.MAX_VALUE) i = Integer.MAX_VALUE;
                return (int)i;
            }
        });
        this.cacheInfos = cacheInfoList;
    }

    @Override
    public int getCount() {
        return cacheInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return cacheInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final CacheInfo info = cacheInfos.get(position);
        ViewHolder holder;
        View view;
        if (convertView == null) {
            view = View.inflate(ctx, R.layout.item_clean_cache, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) view.findViewById(R.id.iv_icon);
            holder.apkName = (TextView) view.findViewById(R.id.tv_name);
            holder.cacheSize = (TextView) view.findViewById(R.id.tv_cache_size);
            holder.root = (LinearLayout) view.findViewById(R.id.rl_root);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.icon.setImageDrawable(info.getIcon());
        holder.apkName.setText(info.getApkName());
        holder.apkName.setSingleLine();
        holder.cacheSize.setText("缓存大小：" + Formatter.formatFileSize(ctx, info.getCacheSize()) +
                "\t\t数据大小：" + Formatter.formatFileSize(ctx, info.getDataSize()));
        /**
         * 点击跳转到设置
         */
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 根据包名跳转到系统自带的应用程序信息界面
                Uri packageURI = Uri.parse("package:" + info.getPackageName());
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                ctx.startActivity(intent);
            }
        });
        return view;
    }

    static class ViewHolder {
        ImageView icon;
        TextView apkName;
        TextView cacheSize;
        LinearLayout root;
    }
}